package com.nfzm.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.nfzm.entity.ArticleVO;
import com.nfzm.entity.NfzmArticle;
import com.nfzm.entity.NfzmArticlePic;
import com.nfzm.entity.NfzmTerm;
import com.nfzm.service.NfzmArticlePicService;
import com.nfzm.service.NfzmArticleService;
import com.nfzm.service.NfzmTermService;
import com.nfzm.tools.HttpTool;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * NfzmArticle的路由接口服务
 * 
 * @author 
 *
 */
@RestController
@Slf4j
public class TestController {

	/** NfzmArticleService服务 */
	@Autowired
	private NfzmArticleService nfzmArticleService;

	@Autowired
	private NfzmArticlePicService picService;

	@Autowired
	private NfzmTermService termService;


	@RequestMapping("/data3")
	public String data(String termId){

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Integer page = 1;

		JSONObject result = new JSONObject();

		JSONObject data = null;

		JSONArray jsonArray = new JSONArray();

		try{
			String url = "https://www.infzm.com/contents?term_id="+termId+"&page="+page+"&format=json";
			result = HttpTool.get(url);
			page++;
			log.info("第几页"+page);
			jsonArray.clear();
			data = result.getJSONObject("data");
			if(null!=data){
				jsonArray = data.getJSONArray("contents");
				log.info("返回数据大小"+jsonArray.size());
				List<NfzmArticle> articleList = new ArrayList<>();

				JSONObject jsonObject =(JSONObject) jsonArray.get(0);

				String id = jsonObject.getString("id");
				String subject = jsonObject.getString("subject");
				String introText = jsonObject.getString("introtext");
				Date date = formatter.parse(jsonObject.getString("publish_time"));

				NfzmArticle article = new NfzmArticle();
				article.setId(id);
				article.setDate(date);
				article.setIntroText(introText);
				article.setTitle(subject);
				article.setTermId(termId);
				article.setInsertTime(new Date());

				nfzmArticleService.saveNotNull(article);

				JSONArray pics = jsonObject.getJSONArray("covers");
				if(null!=pics){
					List<NfzmArticlePic> picList = new ArrayList<>();
					for(int i=0;i<pics.size();i++){
						JSONObject picJson =(JSONObject) pics.get(i);
						NfzmArticlePic pic = new NfzmArticlePic();
						String picUrl = picJson.getString("file_path");
						int width = picJson.getIntValue("media_width");
						int height = picJson.getIntValue("media_height");
						int length = picJson.getIntValue("media_length");
						pic.setArticleId(id);
						pic.setSort(i);
						pic.setWidth(width);
						pic.setHeight(height);
						pic.setLength(length);
						pic.setPicUrl(picUrl);
						pic.setInsertTime(new Date());
						picService.saveNotNull(pic);
					}

				}

			}

		}catch (Exception e){
			log.error("出错了",e);
		}


		log.info("end------------------------------");


		return "0";
	}


}
